CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

# define the project name
PROJECT(SkpSDK)

# High level project configuration
SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/stage" )
SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/stage" )
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/stage" )

# path to C++ SDK
SET (SDK_DIR "C:/working/sdk" CACHE TYPE STRING)

# directories to search for includes
INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR} )
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} )
INCLUDE_DIRECTORIES( "${SDK_DIR}/SkpReader/Interfaces" )
INCLUDE_DIRECTORIES( "${SDK_DIR}/SkpWriter/Headers" )

# Ruby
FIND_PROGRAM( RUBY_EXECUTABLE NAMES ruby PATHS $ENV{CMAKE_RUBY_PATH} NO_SYSTEM_ENVIRONMENT_PATH )
FIND_PACKAGE( Ruby REQUIRED )
INCLUDE_DIRECTORIES( ${RUBY_INCLUDE_DIRS} )

# SWIG
FIND_PACKAGE( SWIG 1.3.40 REQUIRED )

# compiler specific defines
IF(WIN32)
  SET( SWIG_DEFINES "-D_WINDOWS" )
  SET( SWIG_COMMON "-Fmicrosoft" )
ENDIF(WIN32)

# SWIG rule to generate the wrapper file, CMake will understand the dependencies of this wrapper and rerun SWIG when the wrapper is out of date
ADD_CUSTOM_COMMAND(
  OUTPUT "SkpReader_wrap.cxx"
  COMMAND "${SWIG_EXECUTABLE}"
          "-ruby" "-c++" "-fvirtual" "-I${CMAKE_SOURCE_DIR}" "-I${CMAKE_BINARY_DIR}" 
          -module "SUSDK" -initname "skpreader"
          -o "${CMAKE_CURRENT_BINARY_DIR}/SkpReader_wrap.cxx"
          "${SWIG_DEFINES}" ${SWIG_COMMON} "${CMAKE_SOURCE_DIR}/SkpReader.i"
  DEPENDS "${CMAKE_SOURCE_DIR}/SkpReader.i" "${CMAKE_SOURCE_DIR}/SkpReader.hpp"
) 

# add a dynamic library
ADD_LIBRARY(
  skpreader
  MODULE
  SkpReader.i
  SkpReader.hpp
  SkpReader.cpp
  runme.rb
  ${CMAKE_CURRENT_BINARY_DIR}/SkpReader_wrap.cxx
  "${SDK_DIR}/SkpReader/Interfaces/SketchUp_i.h"
  "${SDK_DIR}/SkpReader/Interfaces/SketchUp_i.c"  
  "${SDK_DIR}/SkpReader/Interfaces/SketchUpAddin.h"
  "${SDK_DIR}/SkpReader/Interfaces/SketchUpAddin.c"
  
)

SET(skpreader_depends ${RUBY_LIBRARY} "${SDK_DIR}/SkpReader/Binaries/Windows/SketchUpReader.lib")

# link library to the Ruby and SkpReader libraries
TARGET_LINK_LIBRARIES( skpreader ${skpreader_depends} )

# on Mac need to make sure that we compile in 32 bit for SketchUp and 64 bit for system Ruby
IF(APPLE)
  SET( skpreader PROPERTIES CMAKE_OSX_ARCHITECTURE "i386;x86_64" )
  SET_TARGET_PROPERTIES( skpreader PROPERTIES SUFFIX ".bundle" )
  SET_TARGET_PROPERTIES( skpreader PROPERTIES LINK_FLAGS "-undefined suppress -flat_namespace")
ENDIF(APPLE)

ADD_CUSTOM_TARGET(stage ALL 
  COMMAND ${CMAKE_COMMAND} -E copy "${SDK_DIR}/SkpReader/Binaries/Windows/msvcr80.dll" "${CMAKE_BINARY_DIR}/stage/${CMAKE_CFG_INTDIR}"
  COMMAND ${CMAKE_COMMAND} -E copy "${SDK_DIR}/SkpReader/Binaries/Windows/xerces-c_2_6.dll" "${CMAKE_BINARY_DIR}/stage/${CMAKE_CFG_INTDIR}"
  COMMAND ${CMAKE_COMMAND} -E copy "${SDK_DIR}/SkpReader/Binaries/Windows/gdal12.dll" "${CMAKE_BINARY_DIR}/stage/${CMAKE_CFG_INTDIR}"
  COMMAND ${CMAKE_COMMAND} -E copy "${SDK_DIR}/SkpReader/Binaries/Windows/SketchUpReader.dll" "${CMAKE_BINARY_DIR}/stage/${CMAKE_CFG_INTDIR}"
  COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/runme.rb" "${CMAKE_BINARY_DIR}/stage/${CMAKE_CFG_INTDIR}"
  DEPENDS skpreader "${CMAKE_SOURCE_DIR}/runme.rb"
)
